package kevin.workplan.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import kevin.workplan.database.gen.model.Plan;
import kevin.workplan.database.gen.model.PlanExample;

public class PlanUtil {

	/** 从request中取出参数，生成plan对象 **/
	public static Plan getPlan(HttpServletRequest request) throws ParseException {
		Plan plan = new Plan();
		String bid = request.getParameter("bid");
		if (!Util.empty(bid)) {
			plan.setBid(Long.parseLong(bid));
			request.setAttribute("bid", bid);
		}
		String bname = request.getParameter("bname");
		if (!Util.empty(bname)) {
			plan.setBname(bname);
			request.setAttribute("bname", bname);
		}
		String sid = request.getParameter("sid");
		if (!Util.empty(sid)) {
			plan.setSid(Long.parseLong(sid));
			request.setAttribute("sid", sid);
		}
		String sname = request.getParameter("sname");
		if (!Util.empty(sname)) {
			plan.setSname(sname);
			request.setAttribute("sname", sname);
		}
		String content = request.getParameter("content");
		if(content!=null) {
			plan.setContent(content);
			request.setAttribute("content", content);
		}
		String title = request.getParameter("title");
		if(title!=null) {
			plan.setTitle(title);
			request.setAttribute("title", title);
		}
		Integer priority = PageUtil.getInteger(request,"priority");
		if (priority!=null) {
			plan.setPriority(priority);
			request.setAttribute("priority", priority);
		}
		Integer progress = PageUtil.getInteger(request,"progress");
		if (progress!=null) {
			plan.setProgress(progress);
			request.setAttribute("progress", progress);
		}
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		String startTime = (String) request.getParameter("startTime");
		if (!Util.empty(startTime)){
			plan.setStartTime(sf.parse(startTime));
			request.setAttribute("startTime", startTime);
		}
		String endTime = (String) request.getParameter("endTime");
		if (!Util.empty(endTime)){
			plan.setEndTime(sf.parse(endTime));
			request.setAttribute("endTime", endTime);
		}
		String createTime = (String) request.getParameter("createTime");
		if (!Util.empty(createTime)) {
			plan.setCreateTime(sf.parse(createTime));
			request.setAttribute("createTime", createTime);
		}
		return plan;
	}
	/**
	 * 生成查询条件
	 * @param plan
	 * @return 一个条件都没设返回null
	 * @throws ParseException 
	 */
	public static PlanExample genPlanExample(HttpServletRequest request, Long uid) throws ParseException {
		PlanExample px = new PlanExample();
		PlanExample.Criteria cr = px.createCriteria();
		
		cr.andUidEqualTo(uid);
		String sid = request.getParameter("sid");
		if(!Util.empty(sid)) {
			String[] list = sid.split(",");
			List<Long> slist = new ArrayList<Long>();
			for(String s : list) {
				slist.add(Long.parseLong(s));
			}
			cr.andSidIn(slist);
			request.setAttribute("sid", sid);
		}
		String bname = request.getParameter("bname");
		if (!Util.empty(bname)) {
			cr.andBnameEqualTo(bname);
			request.setAttribute("bname", bname);
		}
		String sname = request.getParameter("sname");
		if (!Util.empty(sname)) {
			cr.andSnameEqualTo(sname);
			request.setAttribute("sname", sname);
		}
		String title = request.getParameter("title");
		if(title!=null) {
			cr.andTitleLike(title);
			request.setAttribute("title", title);
		}
		Integer priority = PageUtil.getInteger(request,"priority");
		if (priority!=null) {
			cr.andPriorityGreaterThanOrEqualTo(priority);
			request.setAttribute("priority", priority);
		}
		Integer progress = PageUtil.getInteger(request,"progress");
		if (progress!=null) {
			cr.andProgressGreaterThanOrEqualTo(progress);
			request.setAttribute("progress", progress);
		}
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		String startTimeBegin = (String) request.getParameter("startTimeBegin");
		if (!Util.empty(startTimeBegin)){
			cr.andStartTimeGreaterThanOrEqualTo(sf.parse(startTimeBegin));
			request.setAttribute("startTimeBegin", startTimeBegin);
		}
		String startTimeEnd = (String) request.getParameter("startTimeEnd");
		if (!Util.empty(startTimeEnd)){
			cr.andStartTimeLessThanOrEqualTo(sf.parse(startTimeEnd));
			request.setAttribute("startTimeEnd", startTimeEnd);
		}
		String endTimeBegin = (String) request.getParameter("endTimeBegin");
		if (!Util.empty(endTimeBegin)){
			cr.andEndTimeGreaterThanOrEqualTo(sf.parse(endTimeBegin));
			request.setAttribute("endTimeBegin", endTimeBegin);
		}
		String endTimeEnd = (String) request.getParameter("endTimeEnd");
		if (!Util.empty(endTimeEnd)){
			cr.andEndTimeLessThanOrEqualTo((sf.parse(endTimeEnd)));
			request.setAttribute("endTimeEnd", endTimeEnd);
		}
		String createTimeBegin = (String) request.getParameter("createTimeBegin");
		if (!Util.empty(createTimeBegin)) {
			cr.andCreateTimeGreaterThanOrEqualTo(sf.parse(createTimeBegin));
			request.setAttribute("createTimeBegin", createTimeBegin);
		}
		String createTimeEnd = (String) request.getParameter("createTimeEnd");
		if (!Util.empty(createTimeEnd)) {
			cr.andCreateTimeLessThanOrEqualTo((sf.parse(createTimeEnd)));
			request.setAttribute("createTimeEnd", createTimeEnd);
		}
		
		
		boolean showComplete = false;	// 默认不显示已完成的
		String showCompleteStr = request.getParameter("showComplete");
		request.setAttribute("showComplete", showCompleteStr);
		if(!Util.empty(showCompleteStr)) {
			showComplete = true;
		}
		if(!showComplete) {
			cr.andProgressNotEqualTo(Constant.PROGRESS_FINISH);
		}
		return px;
	}
}
